<?php
namespace  Core;
use Core\Http;
use Core\Kernel;

class HttpKernel extends Kernel\Kernel
{
    public function filters() {
        return [
            [
              'CROSFilter',
            ],
            [
                'HttpCacheFilter',
            ]
        ];
    }

    public function handlers() {
        return [
            'HttpCacheHandler',
            'TokenHandler',
            'CROSHandler',
            'StatusHandler',
            'LimitHandler',
            'ProxyHander',
            'AuthHandler',
        ];
    }

    public function run($request,$response) {
        return $this->Handler();
    }

    public function LogicHandler() {
        $dispatcher = FastRoute\simpleDispatcher($this->routes);

        // 获取请求的方法和 URI
        $httpMethod = $_SERVER['REQUEST_METHOD'];
        $uri = $_SERVER['REQUEST_URI'];

        // 去除查询字符串( ? 后面的内容) 和 解码 URI
        if (false !== $pos = strpos($uri, '?')) {
            $uri = substr($uri, 0, $pos);
        }
        $uri = rawurldecode($uri);

        $routeInfo = $dispatcher->dispatch($httpMethod, $uri);
        switch ($routeInfo[0]) {
            case FastRoute\Dispatcher::NOT_FOUND:
                // ... 404 Not Found 没找到对应的方法
                break;
            case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
                $allowedMethods = $routeInfo[1];
                // ... 405 Method Not Allowed  方法不允许
                break;
            case FastRoute\Dispatcher::FOUND: // 找到对应的方法
                $handler = $routeInfo[1]; // 获得处理函数
                $vars = $routeInfo[2]; // 获取请求参数
                // ... call $handler with $vars // 调用处理函数
                break;
        }
    }
}